package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowApplyLineRpcService;
import com.hunttown.mes.rpc.domain.WorkflowApplyLineDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowApplyLineDTOQuery;
import com.hunttown.mes.service.WorkflowApplyLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowApplyLineConverts.convert;

/**
 * created by wangjunfu on 2019-08-01 16:55:16
 */
@Service
public class WorkflowApplyLineRpcServiceImpl implements WorkflowApplyLineRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowApplyLineRpcServiceImpl.class);

    private final WorkflowApplyLineService workflowApplyLineService;

    @Autowired
    public WorkflowApplyLineRpcServiceImpl(WorkflowApplyLineService workflowApplyLineService) {
        this.workflowApplyLineService = workflowApplyLineService;
    }

    @Override
    public WorkflowApplyLineDTO insert(WorkflowApplyLineDTO workflowApplyLineDTO) {
        try {
            return convert(workflowApplyLineService.insert(convert(workflowApplyLineDTO)));
        } catch (Exception e) {
            logger.error("WorkflowApplyLine create failed.", e);
            throw new RuntimeException("WorkflowApplyLine create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowApplyLineDTO workflowApplyLineDTO) {
        try {
            return workflowApplyLineService.updateInfoById(convert(workflowApplyLineDTO));
        } catch (Exception e) {
            logger.error("WorkflowApplyLine update by id failed.", e);
            throw new RuntimeException("WorkflowApplyLine update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowApplyLineDTOQuery workflowApplyLineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyLineDTOQuery.getW_id() != null && workflowApplyLineDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowApplyLineDTOQuery.getW_id());
            }

            if (workflowApplyLineDTOQuery.getW_applyId() != null && workflowApplyLineDTOQuery.getW_applyId() > 0) {
                query.put("w_applyId", workflowApplyLineDTOQuery.getW_applyId());
            }

            if (workflowApplyLineDTOQuery.getW_nodeSort() != null && workflowApplyLineDTOQuery.getW_nodeSort() > 0) {
                query.put("w_nodeSort", workflowApplyLineDTOQuery.getW_nodeSort());
            }

            if (workflowApplyLineDTOQuery.getIds() != null && !workflowApplyLineDTOQuery.getIds().equals("")) {
                query.put("ids", workflowApplyLineDTOQuery.getIds());
            }

            if (workflowApplyLineDTOQuery.getIsAuto() != null) {
                query.put("isAuto", workflowApplyLineDTOQuery.getIsAuto());
            }

            if (workflowApplyLineDTOQuery.getStatus() != null) {
                query.put("status", workflowApplyLineDTOQuery.getStatus());
            }

            if (workflowApplyLineDTOQuery.getApprover() != null) {
                query.put("approver", workflowApplyLineDTOQuery.getApprover());
            }

            if (workflowApplyLineDTOQuery.getRealApproverId() != null) {
                query.put("realApproverId", workflowApplyLineDTOQuery.getRealApproverId());
            }

            if (workflowApplyLineDTOQuery.getRealApproverTime() != null) {
                query.put("realApproverTime", workflowApplyLineDTOQuery.getRealApproverTime());
            }

            if (StringUtils.isNotBlank(workflowApplyLineDTOQuery.getRemark())) {
                query.put("remark", workflowApplyLineDTOQuery.getRemark());
            }

            if (workflowApplyLineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowApplyLineDTOQuery.getStopFlag());
            }

            if (workflowApplyLineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowApplyLineDTOQuery.getDeleteFlag());
            }

            return workflowApplyLineService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowApplyLine update by query failed.", e);
            throw new RuntimeException("WorkflowApplyLine update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyLineDTO selectById(Integer id) {
        try {
            return convert(workflowApplyLineService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowApplyLine select by id failed.", e);
            throw new RuntimeException("WorkflowApplyLine select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyLineDTO selectObjByQuery(WorkflowApplyLineDTOQuery workflowApplyLineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyLineDTOQuery.getApplyId() != null && workflowApplyLineDTOQuery.getApplyId() > 0) {
                query.put("applyId", workflowApplyLineDTOQuery.getApplyId());
            }

            if (workflowApplyLineDTOQuery.getBusinessId() != null && workflowApplyLineDTOQuery.getBusinessId() > 0) {
                query.put("businessId", workflowApplyLineDTOQuery.getBusinessId());
            }

            if (workflowApplyLineDTOQuery.getNodeSort() != null) {
                query.put("nodeSort", workflowApplyLineDTOQuery.getNodeSort());
            }

            if (workflowApplyLineDTOQuery.getNodeSign() != null) {
                query.put("nodeSign", workflowApplyLineDTOQuery.getNodeSign());
            }

            if (workflowApplyLineDTOQuery.getIsCurrent() != null) {
                query.put("isCurrent", workflowApplyLineDTOQuery.getIsCurrent());
            }

            if (workflowApplyLineDTOQuery.getStatus() != null) {
                query.put("status", workflowApplyLineDTOQuery.getStatus());
            }

            if (StringUtils.isNotBlank(workflowApplyLineDTOQuery.getOrderBy())) {
                query.put("orderBy", workflowApplyLineDTOQuery.getOrderBy());
            }

            if (workflowApplyLineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowApplyLineDTOQuery.getStopFlag());
            }

            if (workflowApplyLineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowApplyLineDTOQuery.getDeleteFlag());
            }

            return convert(workflowApplyLineService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowApplyLine select obj by query failed.", e);
            throw new RuntimeException("WorkflowApplyLine select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowApplyLineDTO> getListForPage(WorkflowApplyLineDTOQuery workflowApplyLineDTOQuery) {
        Query query = Query.page(workflowApplyLineDTOQuery.getPn(), workflowApplyLineDTOQuery.getPageSize());

        if (workflowApplyLineDTOQuery.getId() != null && workflowApplyLineDTOQuery.getId() > 0) {
            query.put("id", workflowApplyLineDTOQuery.getId());
        }

        if (workflowApplyLineDTOQuery.getApplyId() != null && workflowApplyLineDTOQuery.getApplyId() > 0) {
            query.put("applyId", workflowApplyLineDTOQuery.getApplyId());
        }

        if (workflowApplyLineDTOQuery.getIds() != null && !workflowApplyLineDTOQuery.getIds().equals("")) {
            query.put("ids", workflowApplyLineDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(workflowApplyLineDTOQuery.getLikeName())) {
            query.put("likeName", workflowApplyLineDTOQuery.getLikeName());
        }

        if (workflowApplyLineDTOQuery.getIsCanBack() != null) {
            query.put("isCanBack", workflowApplyLineDTOQuery.getIsCanBack());
        }

        if (workflowApplyLineDTOQuery.getBusinessId() != null) {
            query.put("businessId", workflowApplyLineDTOQuery.getBusinessId());
        }

        if (workflowApplyLineDTOQuery.getNodeSort() != null) {
            query.put("nodeSort", workflowApplyLineDTOQuery.getNodeSort());
        }

        if (workflowApplyLineDTOQuery.getNodeSign() != null) {
            query.put("nodeSign", workflowApplyLineDTOQuery.getNodeSign());
        }

        if (workflowApplyLineDTOQuery.getBeforeNodeSort() != null) {
            query.put("beforeNodeSort", workflowApplyLineDTOQuery.getBeforeNodeSort());
        }

        if (workflowApplyLineDTOQuery.getAfterNodeSort() != null) {
            query.put("afterNodeSort", workflowApplyLineDTOQuery.getAfterNodeSort());
        }

        if (workflowApplyLineDTOQuery.getApprovalType() != null) {
            query.put("approvalType", workflowApplyLineDTOQuery.getApprovalType());
        }

        if (workflowApplyLineDTOQuery.getStatus() != null) {
            query.put("status", workflowApplyLineDTOQuery.getStatus());
        }

        if (StringUtils.isNotBlank(workflowApplyLineDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowApplyLineDTOQuery.getOrderBy());
        }

        if (workflowApplyLineDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", workflowApplyLineDTOQuery.getStopFlag());
        }

        if (workflowApplyLineDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", workflowApplyLineDTOQuery.getDeleteFlag());
        }

        List<WorkflowApplyLineDTO> list = convert(workflowApplyLineService.selectByQuery(query));
        Page<WorkflowApplyLineDTO> page = Page.from(workflowApplyLineDTOQuery.getPn(), workflowApplyLineDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowApplyLineService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowApplyLineService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowApplyLine delete failed.", e);
            throw new RuntimeException("WorkflowApplyLine delete failed, Please order your code.", e);
        }
    }
}